<?php

namespace app\admin\controller;

use app\helpers\Common;

use support\Request;
use support\View;
use think\facade\Db;
use think\Exception;

class AdminMenuController extends BaseController
{

    protected function initialize()
    {
        parent::initialize();

        $request = request();

        //$this->indexView = 'adminmenu:list';
        //$this->addView = 'adminmenu:add';
        //$this->editView = 'adminmenu:edit';

        if ($request->action == 'add' || $request->action == 'edit') {
            $menuOptions = Common::makeTreeOptions(
                \app\common\model\AdminMenu::class,
                [['status', '<>', 90]],
                ['sorting' => 'asc', 'id' => 'asc'],
                ['顶级'],
                0,
                'name'
            );

            View::assign('menuOptions', $menuOptions);

            View::assign('params', $request->all());
        }
    }

    /**
     * List
     */
    public function index(Request $request)
    {
        $param = $request->all();

        $className = '\\app\\' . $this->appName . '\\model\\' . $this->model;
        $model = new $className;

        $data = Common::getTreeList($model, [['id', '>', 0], ['status', '<>', '90']], ['id' => 'asc'], 0, 'name', 'parent_id', true);

        if ($data) {
            // 处理列表数据
            $data = $this->handleListData($data);
        }

        return view(strtolower($this->model) . '/' . $this->indexView, [
            'datas' => $data
        ]);
    }

    /**
     * List
     */
    public function ajaxIndex(Request $request)
    {
        $param = $request->all();

        if ($request->isAjax()) {
            $className = '\\app\\' . $this->appName . '\\model\\' . $this->model;
            $model = new $className;

            $where = [
                ['status', '<>', '90']
            ];
            $data = $model->where($where)->order('id', 'asc')->select();

            $result = [
                'code' => '0',
                'msg' => '',
                'data' => $data,
            ];

            return json($result);
        }

        return view(strtolower($this->model) . '/' . 'ajax_index');
    }

    /**
     * Delete
     */
    public function delete(Request $request)
    {
        //return parent::delete($request);
    }
}
